package service

import (
	"singo/model"
	"singo/serializer"
)

// ListMistakeService 错误日志列表服务
type ListMistakeService struct {
	Limit  int  `form:"pageSize"`
	Start  int  `form:"pageNo"`
	Status uint `form:"status"`
}

// MistakeList 错误日志列表
func (service *ListMistakeService) MistakeList() serializer.Response {
	mistakes := []model.Mistake{}
	total := 0
	if service.Limit == 0 {
		service.Limit = 10
	}

	if err := model.DB.Model(&mistakes).Where(&model.Mistake{Status: service.Status}).Count(&total).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Where(&model.Mistake{Status: service.Status}).Order("status, updated_at desc").Find(&mistakes).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}
	return serializer.BuildListResponse(serializer.BuildMistakes(mistakes), uint(total))
}
